package com.clas.liu.myapplication.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.notice.Notice;
import com.clas.liu.myapplication.view.util.DateUtil;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.widget.CircleImageView;

import java.util.Date;
import java.util.List;

/**
 * Created by liu on 2019/5/20.
 */

public class NoticeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_HEADER = -1;
    private List<Notice> data;
    private Context context;
    private boolean isDelete = false;

    public NoticeAdapter(List<Notice> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_ITEM:
                holder = new MyHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_notices,parent,false));
                break;
            case TYPE_FOOTER:
                holder = new FooterHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_footer_view,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                break;
            case TYPE_FOOTER:
                FooterHolder holder = (FooterHolder)holder1;
                holder.foot_view_item_tv.setText("正在加载...");
                if(isDelete){
                    holder.progressBar.setVisibility(View.GONE);
                    holder.foot_view_item_tv.setText("没有更多了哦~");
                    holder.foot_view_item_tv.setAlpha(0.5f);
                    holder.foot_view_item_tv.setTextColor(context.getResources().getColor(R.color.black));
                }
                if(!NetworkHelper.isNetworkAvailable(context) && data.size()==0){
                    holder.progressBar.setVisibility(View.GONE);
                    holder.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder.loadLayout.setVisibility(View.GONE);
                }else{
                    holder.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder.loadLayout.setVisibility(View.VISIBLE);
                }
                break;
            case TYPE_ITEM:
                MyHolder holder2 = (MyHolder)holder1;
                final Notice info = data.get(position);
                Date date = new Date(Long.parseLong(info.getTime()));
                holder2.time.setText(DateUtil.getFormatTime(date));
                holder2.text.setText(info.getText());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return data.size()+1;
    }

    public void update(List<Notice> root){
        data = root;
        this.notifyDataSetChanged();
    }

    public void deleteLoadingMore(boolean isDelete){
        this.isDelete = isDelete;
        notifyItemChanged(getItemCount()-1);
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else
            return TYPE_ITEM;
    }


    public class MyHolder extends RecyclerView.ViewHolder{

        TextView time;
        TextView text;
        public MyHolder(View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.notice_time);
            text = itemView.findViewById(R.id.notice_text);
        }
    }
    public class FooterHolder extends RecyclerView.ViewHolder{
        private TextView foot_view_item_tv;
        private LinearLayout loadLayout;
        private ProgressBar progressBar;

        public FooterHolder(View view) {
            super(view);
            foot_view_item_tv = (TextView) view.findViewById(R.id.tv);
            loadLayout = (LinearLayout)view.findViewById(R.id.loadmore);
            progressBar = (ProgressBar)view.findViewById(R.id.progress);
        }
    }
}
